package com.penglecode.netty3.example2;

import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.util.concurrent.Executors;

import org.jboss.netty.bootstrap.ServerBootstrap;
import org.jboss.netty.channel.ChannelFactory;
import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.channel.ChannelPipelineFactory;
import org.jboss.netty.channel.Channels;
import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;

import com.penglecode.netty.codec.protostuff.ProtostuffObjectDecoder;
import com.penglecode.netty.codec.protostuff.ProtostuffObjectEncoder;
import com.penglecode.protostuff.runtime.ProtostuffSerializer;

public class MessageServer {
	
	public static void main(String[] args) throws Exception {
		ChannelFactory channelFactory = new NioServerSocketChannelFactory(Executors.newCachedThreadPool(), Executors.newCachedThreadPool());
		ServerBootstrap bootstrap = new ServerBootstrap(channelFactory);
		bootstrap.setOption("child.tcpNoDelay", true);
		bootstrap.setOption("child.keepAlive", true);
		bootstrap.setPipelineFactory(new ChannelPipelineFactory(){
			public ChannelPipeline getPipeline() throws Exception {
				ChannelPipeline channelPipeline = Channels.pipeline();
				channelPipeline.addLast("serverdecoder", new ProtostuffObjectDecoder(new ProtostuffSerializer()));
				channelPipeline.addLast("serverencoder", new ProtostuffObjectEncoder(new ProtostuffSerializer()));
				channelPipeline.addLast("serverhandler", new ServerMessageHandler());
				return channelPipeline;
			}
		});
		bootstrap.bind(new InetSocketAddress(InetAddress.getLocalHost(), 9000));
	}

}
